import { _decorator, Component, Label, Node, Sprite } from 'cc';
import { UIManager } from '../common/UIManager';
import { PrefabEnum } from '../common/PrefabEnum';
import { Hero, Stone } from '../entity/Hero';
import HeroApi from '../api/HeroApi';
import { HeroConfig } from '../settings/const';
import { HeroList } from './HeroList';
import { HeroDetail } from './HeroDetail';
import { EventManager } from '../event/EventManager';
import { AudioManager, Mp3Enum } from '../common/AudioManager';
const { ccclass, property } = _decorator;

@ccclass('Strengthen')
export class Strengthen extends Component {
    @property(Node)
    mask:Node
    @property(Node)
    close:Node
    @property(Label)
    star1:Label
    @property(Node)
    curStarLayout:Node
    @property(Node)
    nextStarLayout:Node
    @property(Node)
    arrow:Node
  
    @property(Label)
    level1:Label
    @property(Label)
    star2:Label
   
    @property(Label)
    level2:Label
    @property(Sprite)
    stoneImg:Sprite
    @property(Label)
    stoneName:Label
    @property(Label)
    stoneNum:Label
    @property(Label)
    stoneCurNum:Label
    @property(Node)
    moreStone:Node
    @property(Node)
    submit:Node
    @property(Node)
    noEnough:Node

    private hero:Hero
    start() {
        this.close.on(Node.EventType.TOUCH_END, this.onClose, this)
        this.mask.on(Node.EventType.TOUCH_END, this.onClose, this)
        this.submit.on(Node.EventType.TOUCH_END, this.onSubmit, this)
        this.moreStone.on(Node.EventType.TOUCH_END, this.onMoreStone, this)

        EventManager.instance.on(EventManager.EventType.RefreshStone, this.onRefreshStone, this)
    }

    async setData(hero:Hero){
        console.log(hero)
        this.hero = hero
        this.star1.string = hero.en + '星'
      
        this.level1.string = hero.en * 10 + ''

        this.star2.string = (hero.en + 1) + '星'
     
        this.level2.string = (hero.en + 1) * 10 + ''

        this.stoneImg.spriteFrame = await hero.getHeadImg()
        this.stoneName.string = hero.name + '强化魂石'
        this.stoneCurNum.string = hero.p + ''
        this.stoneNum.string = '/ ' + hero.c_en 

        if(hero.p >= hero.c_en){
            this.submit.active = true
            this.noEnough.active = false
        } else {
            this.submit.active = false
            this.noEnough.active = true
        }

        if(hero.en >= hero.maxStar){
            this.nextStarLayout.active = false
            this.arrow.active = false
            this.curStarLayout.setPosition(0, this.curStarLayout.position.y)

            this.submit.getChildByName('Label').getComponent(Label).string = '已经满星级了'
        }
    }

    onClose(){
        UIManager.instance.closeUI(PrefabEnum.HeroStrengthen)
    }

    onMoreStone(){
        
        UIManager.instance.openUI(PrefabEnum.HeroStone)
    }

    async onSubmit(){
        if(this.hero.en >= this.hero.maxStar){
            UIManager.instance.tip('已经是最大星级了')
            return
        }

        await UIManager.instance.showLoading()
        let res = await HeroApi.Execute({op:3, hid: this.hero.id})
        UIManager.instance.closeLoading()
        console.log(res)

        AudioManager.instance.playOneShot(Mp3Enum.hero_addstar)

        let adChip = res.data.adChip
        let chip = res.data.chip
        let items = res.data.items

        let json = {
            ...HeroConfig[this.hero.id],
            ...items[this.hero.id]
        }
        let hero = Hero.create(json)

        HeroList.instance.setData(res.data)
        HeroDetail.instance.setData(hero)
        this.setData(hero)
    }

    onRefreshStone(stoneList:Stone[]){
        for(let s of stoneList){
            if(s.heroId == this.hero.id){
                this.hero.p += s.count

                this.stoneCurNum.string = this.hero.p + ''
            }
        }
    }
    
}


